
/**
 * register form element plugins
 */
(function() {
	'use strick',
	
	require.config({
		paths: {
			"form.plugin.ajax": "miwawa/1.0.2/js/jquery/form/3.51.0/js/jquery.form",
			"form.plugin.chosen": "miwawa/1.0.2/js/core/module/formplugin/plugins/chosen",
			"form.plugin.rating": "miwawa/1.0.2/js/core/module/formplugin/plugins/rating",
			"form.plugin.daterange": "miwawa/1.0.2/js/core/module/formplugin/plugins/daterange",
			"form.plugin.datepicker": "miwawa/1.0.2/js/core/module/formplugin/plugins/datepicker",
			"form.plugin.imagepicker": "miwawa/1.0.2/js/core/module/formplugin/plugins/imagepicker",
			"form.plugin.fileinput": "miwawa/1.0.2/js/core/module/bootstrap/fileinput",
			"form.plugin.summernote": "miwawa/1.0.2/js/core/module/bootstrap/summernote"
		}
	});
	
	define(['jquery','form.plugin.ajax'], function(jQuery){
		return {
			chosen:function(elem, options){
				require(['form.plugin.chosen'],function(chosen){
					chosen.register(elem, options);
				});
				return this;
			},
			region:function(elem, options, callback){
				var target = $(elem);
				
				jQuery.ajaxsend({
					url: "/miwawa//saas/common/get",
					data: options,
					success: function(result, status, xhr){
						for(var i = 0, size = result.length; i < size; i++){
							var option = $("<option></option>");
							option.val(result[i].value);
							option.text(result[i].text);
							if(result[i].attributes){
								jQuery.each(result[i].attributes, function(key, val){
									option.data(key, val);
								});
							}
							target.append(option);
						}
						target.trigger('chosen:updated');
						if(callback) callback(target);
					}
				});
			},
			select:function(elem, options, callback){
				// declare options
				var $select = jQuery(elem), _data = $select.data();
				var _opts = jQuery.extend({},{
					url: _data.url || "/miwawa/{dbname}/common/select.json".replace("{dbname}", dbname),
					data: [],
					querys: {"pcode_EQ": _data.pcode}
				},options);
				
				// switch function
				if(_opts.data.length == 0){
					setRemote($select, _data.entity, _opts.querys);
				}else{
					setLocal($select, _opts.data);
				}
				
				// declare set local data
				function setLocal(target, data){
					if(typeof data == "object"){
						setData(target, data, callback);
					}
				}
				
				// declare set remote data
				function setRemote(target, entity, querys){
					jQuery.ajaxsend({
						url: _opts.url,
						cache: true,
						async: false,
						data: {
							"entityClass":entity,
							"bean":querys
						},
						success: function(result, status, xhr){
							setData(target, result, callback);
						}
					});
				}
				
				// declare set select element sub option`s element
				function setData(target, result, callback){
					for(var i = 0, size = result.length; i < size; i++){
						var option = $("<option></option>");
						var obj = result[i];
						if(typeof obj == "object"){
							option.val(obj.value);
							option.text(obj.text);
							if(obj.attributes){
								jQuery.each(result[i].attributes, function(key, val){
									option.data(key, val);
								});
							}
						}else{
							option.val(obj);
							option.text(obj);
						}
						
						target.append(option);
					}
					target.trigger('chosen:updated');
					
					if(callback) callback(target);
				}
			},
			rating:function(elem, options){
				require(['form.plugin.rating'],function(rating){
					rating.register(elem, options);
				});
			},
			daterange:function(elem, options){
				require(['form.plugin.daterange'],function(daterange){
					daterange.register(elem, options);
				});
			},
			datepicker:function(elem, options, others){
				require(['form.plugin.datepicker'],function(datepicker){
					datepicker.register(elem, options, others);
				});
			},
			imagepicker:function(elem, options){
				require(['form.plugin.imagepicker'],function(imagepicker){
					imagepicker.register(elem, options);
				});
			},
			summernote:function(elem, options){
				require(['form.plugin.summernote'],function(summernote){
					summernote.register(elem, options);
				});
			},
			fileinput:function(elem, options){
				require(['form.plugin.fileinput'],function(fileinput){
					fileinput.register(elem, options)
							 .on('filepreajax', options.onUploadBefore || function(){})
							 .on('fileuploaded', options.onUploadSuccess || function(){});
				});
			},
			tooltip:function(elem, options){
				jQuery(elem).tooltip(jQuery.extend({html:true, container:"html", trigger:'hover'},options));
			}
		};
	});
})(this);